<template>
  <div class="order-coin-container">
    <el-card class="order-coin-card">
      <template #header>
        <div class="card-header">
          <span>币券订单管理</span>
          <div class="header-operations">
            <el-input
              v-model="searchQuery"
              placeholder="搜索订单号/用户名"
              style="width: 200px; margin-right: 10px;"
            />
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
        </div>
      </template>
      
      <el-table :data="orderList" style="width: 100%">
        <el-table-column prop="id" label="订单号" width="180" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="coinAmount" label="币券数量" width="100" />
        <el-table-column prop="amount" label="支付金额" width="100">
          <template #default="scope">
            ¥{{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="payMethod" label="支付方式" width="100" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === '待支付' ? 'warning' : scope.row.status === '已支付' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleDetail(scope.row)">详情</el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleCancel(scope.row)"
              v-if="scope.row.status === '待支付'"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 订单详情对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="订单详情"
      width="600px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="订单号">{{ currentOrder.id }}</el-descriptions-item>
        <el-descriptions-item label="用户名">{{ currentOrder.userName }}</el-descriptions-item>
        <el-descriptions-item label="币券数量">{{ currentOrder.coinAmount }}</el-descriptions-item>
        <el-descriptions-item label="支付金额">¥{{ currentOrder.amount }}</el-descriptions-item>
        <el-descriptions-item label="支付方式">{{ currentOrder.payMethod }}</el-descriptions-item>
        <el-descriptions-item label="订单状态">
          <el-tag :type="currentOrder.status === '待支付' ? 'warning' : currentOrder.status === '已支付' ? 'success' : 'danger'">
            {{ currentOrder.status }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间" :span="2">{{ currentOrder.createTime }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索相关
const searchQuery = ref('')

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 订单列表数据
const orderList = ref([
  {
    id: 'CO202404050001',
    userName: '用户A',
    coinAmount: 100,
    amount: 10.00,
    payMethod: '微信支付',
    createTime: '2024-04-05 10:00:00',
    status: '待支付'
  },
  {
    id: 'CO202404050002',
    userName: '用户B',
    coinAmount: 200,
    amount: 20.00,
    payMethod: '支付宝',
    createTime: '2024-04-05 11:00:00',
    status: '已支付'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const currentOrder = ref({})

// 搜索
const handleSearch = () => {
  // TODO: 调用搜索接口
  console.log('搜索关键词：', searchQuery.value)
}

// 查看详情
const handleDetail = (row) => {
  currentOrder.value = row
  dialogVisible.value = true
}

// 取消订单
const handleCancel = (row) => {
  ElMessageBox.confirm(
    '确定要取消该订单吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用取消订单接口
    ElMessage.success('订单已取消')
  }).catch(() => {})
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
}
</script>

<style scoped lang="scss">
.order-coin-container {
  padding: 20px;
}

.order-coin-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .header-operations {
    display: flex;
    align-items: center;
  }
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>